<template>
  <el-dialog
    :title="title"
    :width="width"
    top="50px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="isVisible"
  >
    <el-form
      ref="book"
      :model="book"
      :rules="rules"
      label-position="right"
      label-width="100px"
    >
      <el-form-item :label="'图书名称'" prop="name">
        <el-input v-model="book.name" />
      </el-form-item>
      <el-form-item align="center" label="图书作者" prop="author">
        <el-input v-model="book.author" />
      </el-form-item>
      <el-form-item align="center" label="出版社" prop="publish">
        <el-input v-model="book.publish" />
      </el-form-item>
      <el-form-item align="left" label="isbn" prop="isbn">
        <el-input v-model="book.isbn" />
      </el-form-item>
      <el-form-item align="left" label="语言" prop="language">
        <el-select v-model="book.language" placeholder="请选择语言">
          <el-option
            v-for="item in language"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item align="left" label="图书类别" prop="classId">
        <el-select
          v-model="book.lclassInfo.classId"
          placeholder="请选择图书类别"
        >
          <el-option
            v-for="item in classList"
            :key="item.classId"
            :label="item.className"
            :value="item.classId"
          />
        </el-select>
      </el-form-item>
      <el-form-item align="center" label="图书价格" prop="price">
        <el-input v-model="book.price" />
      </el-form-item>
      <el-form-item align="center" label="图书摘要" prop="introduction">
        <el-input v-model="book.introduction" type="textarea" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        type="warning"
        plain
        :loading="buttonLoading"
        @click="isVisible = false"
      >
        {{ $t("common.cancel") }}
      </el-button>
      <el-button
        type="primary"
        plain
        :loading="buttonLoading"
        @click="submitForm"
      >
        {{ $t("common.confirm") }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { validMobile } from '@/utils/my-validate'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'BookEdit',
  components: { Treeselect },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      classList: [],
      language: [
        {
          value: '中文',
          label: '中文'
        },
        {
          value: '英文',
          label: '英文'
        },
        {
          value: '德语',
          label: '德语'
        },
        {
          value: '法语',
          label: '法语'
        }
      ],
      book: this.initBook(),
      buttonLoading: false,
      screenWidth: 0,
      width: this.initWidth(),
      depts: [],
      roles: [],
      rules: {
        name: { required: true, message: this.$t('rules.require'), trigger: 'blur' },
        author: { required: true, message: this.$t('rules.require'), trigger: 'blur' },
        publish: { required: true, message: this.$t('rules.require'), trigger: 'blur' },
        isbn: { required: true, message: this.$t('rules.require'), trigger: 'blur' },
        introduction: { required: true, message: this.$t('rules.require'), trigger: 'blur' },
        language: { required: true, message: this.$t('rules.require'), trigger: 'blur' },
        price: { required: true, message: this.$t('rules.require'), trigger: 'blur' }
      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.dialogVisible
      },
      set() {
        this.close()
        this.reset()
      }
    }
  },
  mounted() {
    this.initClass()
    this.initLanguage()
    window.onresize = () => {
      return (() => {
        this.width = this.initWidth()
      })()
    }
  },
  methods: {
    initBook() {
      return {
        bookId: '',
        name: '',
        author: '',
        publish: '',
        isbn: '',
        introduction: '',
        language: null,
        price: '',
        classId: null,
        lclassInfo: {
          classId: '',
          className: ''
        }
      }
    },
    initWidth() {
      this.screenWidth = document.body.clientWidth
      if (this.screenWidth < 991) {
        return '90%'
      } else if (this.screenWidth < 1400) {
        return '45%'
      } else {
        return '800px'
      }
    },
    initClass() {
      this.$get('study/lClassInfo').then(res => {
        this.classList = res.data.resultBody;
      });
    },
    initLanguage() {
      this.language = [
        {
          value: '中文',
          label: '中文'
        },
        {
          value: '英文',
          label: '英文'
        },
        {
          value: '德语',
          label: '德语'
        },
        {
          value: '法语',
          label: '法语'
        }
      ];
    },
    setBook(val) {
      console.log(val)
      this.book = { ...val }
    },
    close() {
      this.$emit('close')
    },
    submitForm() {
      this.$refs.book.validate((valid) => {
        this.book.classId = this.book.lclassInfo.classId
        if (valid) {
          this.buttonLoading = true
          if (!this.book.bookId) {
            // create
            this.$post('study/lBookInfo', { ...this.book }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.createSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          } else {
            // update
            this.$put('study/lBookInfo', { ...this.book }).then(() => {
              this.buttonLoading = false
              this.isVisible = false
              this.$message({
                message: this.$t('tips.updateSuccess'),
                type: 'success'
              })
              this.$emit('success')
            })
          }
        } else {
          return false
        }
      })
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.$refs.book.clearValidate()
      this.$refs.book.resetFields()
      this.book = this.initBook()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
